package org.yzz.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yzz.auth.annotation.RonghuaPermission;
import org.yzz.base.utils.AjaxResult;
import org.yzz.base.utils.PageInfo;
import org.yzz.org.domain.Employee;
import org.yzz.org.dto.EmployeeDTO;
import org.yzz.org.query.DepartmentQuery;
import org.yzz.org.service.IEmployeeService;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/employee")
@RonghuaPermission(name = "员工管理")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    @RonghuaPermission(name = "查询全部员工")
    @ApiOperation(value = "查询全部员工" )
    public AjaxResult selectAll(){
        try {
            List<Employee> employees = employeeService.selectAll();
            return AjaxResult.success(employees);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @GetMapping("/{id}")
    @RonghuaPermission(name = "通过ID查询员工")
    @ApiOperation(value = "通过ID查询员工" )
    public AjaxResult selectById(@PathVariable Long id){
        try {
            Employee employee = employeeService.selectById(id);
            return AjaxResult.success(employee);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    //通过名字查询
    /*@GetMapping("/{username}")
    public void selectByName(String username){

    }*/

    /**
     * 新增或修改
     * @param employee
     */
    @PostMapping
    @RonghuaPermission(name = "员工新增或修改")
    @ApiOperation(value = "新增或修改" )
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if(Objects.nonNull(employee.getId())){
                employeeService.update(employee);
                return AjaxResult.success();
            }
            employeeService.insert(employee);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "员工根据指定 id 删除员工")
    @ApiOperation(value = "删除" )
    public AjaxResult delete(@PathVariable Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param departmentQuery
     * @return
     */
    @PutMapping
    @RonghuaPermission(name = "员工分页查询")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery){
        try {
            PageInfo<Employee> pageInfo = employeeService.queryPage(departmentQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
    @PatchMapping
    @RonghuaPermission(name = "员工批量删除")
    public  AjaxResult batchDelete(@RequestBody  List<Long> ids){
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     * 保存角色
     * @param
     * @return
     */
    @PostMapping("/role")
    @RonghuaPermission(name = "保存角色")
    @ApiOperation(value = "保存角色" )
    public AjaxResult insertByRoleId(@RequestBody EmployeeDTO employeeDTO){
        try {
             employeeService.insertByRoleId(employeeDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/role/{id}")
    @RonghuaPermission(name = "通过员工ID查询角色id 集合")
    @ApiOperation(value = "通过员工ID查询角色id 集合" )
    public AjaxResult selectRoleById(@PathVariable Long id){
        try {
            List<Long> ids=employeeService.selectRoleById(id);
            return AjaxResult.success(ids);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
